<template>
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%"
            :close-on-click-modal="false"
            >
        <el-form ref="editForm" :rules="rules" :model="editUser" label-width="80px">
            <el-form-item label="学号">
                <el-input v-model="editUser.sn"></el-input>
            </el-form-item>
            <el-form-item  label="密码">
                <el-input v-model="editUser.password"></el-input>
            </el-form-item>
            <el-form-item label="名字" prop="name">
                <el-input v-model="editUser.name"></el-input>
            </el-form-item>
            <el-form-item label="权限">
                <el-select v-model="editUser.roleId" placeholder="请选择权限"  style="width: 100%">
                    <el-option
                            v-for="item in roles"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="头像">
                <el-upload
                    class="avatar-uploader"
                    action="/api/file/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
    </el-dialog>
</template>

<script>
import {editUser} from '../../../../api/user'

export default {
  name: 'UserEditDialog',
  data: function () {
    return {
      dialogVisible: false,
      editUser: {},
      roles: [],
      rules: {
        name: [{required: true, message: '姓名不能为空', trigger: 'blur'}]
      },
      imageUrl: ''
    }
  },

  methods: {
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.editUser.url = res.url
    },
    show (user, roles) {
      this.editUser = JSON.parse(JSON.stringify(user))
      this.roles = roles
      this.imageUrl = this.editUser.url
      this.dialogVisible = true
    },
    submit () {
      this.$refs.editForm.validate(flag => {
        if (flag) {
          editUser(this.editUser).then(res => {
            if (res.data) {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogVisible = false
              this.$emit('ok')
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
/deep/ .el-upload{
    border: 1px dotted;
}
</style>
